<?php
//copie des fichiers
include($project_path.'/install_report.php5');
$error = false;
if(!isset($_SESSION['install']['package']))
{
	echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_NOVERSIONS»");';
	$error = true;
}
else
{
	$temp_path = $project_path.'/temp/';
	$file = $_SESSION['install']['package']['name'].'.'.str_replace('.', '_', $_SESSION['install']['package']['version']).'.update.tgz';
	if(!file_exists($temp_path.$file) || filesize($temp_path.$file)!=$_SESSION['install']['package']['size'] || md5_file($temp_path.$file)!=$_SESSION['install']['package']['md5'])
	{
		echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_DOWNLOADING»");';
		$error = true;
	}
}
if(!$error)
{
	if(isset($_SESSION['install']['memo']))
	{
		list($index, $sqls) = $_SESSION['install']['memo'];
		unset($_SESSION['install']['memo']);
	}
	else
	{
		include_once($project_path.'/class/ext/archive/gzip.class.php5');
		$package = $_SESSION['install']['package'];
	
		$setup = @new SimpleXMLElement($package['index']);
		$file_sql = $setup->screens[0]->createtable[0]['path'];
		if($file_sql!='')
		{
			$file = $package['name'].'.'.str_replace('.', '_', $package['version']).'.update.tgz';
			$tgz = new gzip_file($temp_path.$file);
			$tgz->set_options(array('basedir' => $temp_path, 'overwrite' => 1));
		
			list($errors, $n_succes) = $tgz->extract_files(array(array('', $file_sql)));
			
			if(count($errors)>0 || !is_file($temp_path.'/'.$file_sql))
			{
				echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_EXTRACTING»");';
				$error = true;
			}
			else
			{
				$sqls =  explode(';
', file_get_contents($temp_path.'/'.$file_sql));
	
				@unlink($temp_path.'/'.$file_sql);
			}
			$index = 0;
		}
		else
		{
			$error = true;
		}
	}
}
if(!$error)
{
	fwDb::setData('SET FOREIGN_KEY_CHECKS=0');
	
	session_write_close();
	$nb = count($sqls);
	$last_progree= microtime(true);
	fwDbEngin::$dieOnError = false;
	
	for($i=$index; $i<$nb; $i++)
	{
		$sql = $sqls[$i];
		if((microtime(true)-$last_progree)*1000>470)
		{
			session_start();
			if((microtime(true)-$start_time)>23)
			{
				$_SESSION['install']['memo'] = array($index, $sqls);
				echo 'updatePerform();';
				$error = true;
				break;
			}
			$_SESSION['install']['progress'] = round(($index*1000)/$nb);
			if($_SESSION['install']['cancel'])
			{
				$error = true;
				break;
			}
			session_write_close();
			$last_progree= microtime(true);
		}
		
		$sql = trim($sql);
		if($sql=='')continue;
		
		$search = 'ttprefix__';
		$replace = $conf['bdd']['prefix'];
		$sql = str_replace($search, $replace, $sql);
		
		if($_SERVER['HTTP_HOST']!='tt')
		{
			if(fwDb::setData($sql)===false)
			{
				echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_EXECUTINGSQL»");';
				$error = true;
				fwDb::setData('SET FOREIGN_KEY_CHECKS=1');
				break;
			}
		}
	}
	fwDb::setData('SET FOREIGN_KEY_CHECKS=1');
}
if(!$error)
{
	session_start();
	$next_screen = array_shift($_SESSION['install']['screens']);
	$_SESSION['install']['screen'] = $next_screen;
	$_SESSION['install']['progress'] = 0;
	$_SESSION['install']['status'] = '';
	echo 'updateUpdateStepCurrent("'.$_SESSION['install']['package']['name'].'", "'.$_SESSION['install']['package']['version'].'", '.$next_screen.')
updatePerform("'.$_SESSION['install']['id'].'", "«!ADMIN_INSTALL_'.$install_pages[$next_screen].'_HEAD»", "«!ADMIN_INSTALL_'.$install_pages[$next_screen].'_STATUS»");';
	if(in_array($next_screen, array(5,6)))
	{
		echo 'updateStartProgress()';
	}
}
?>